package com.aiti.lulian.strategy.task;



import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardUserIncreaseRankDto;
import com.aiti.lulian.entity.CardRankTaskUserComplete;
import com.aiti.lulian.entity.CardUserAuthentication;
import com.aiti.lulian.entity.CardUserRankTaskHistory;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.enums.RankIncreaseTaskStrategyEnum;
import com.aiti.lulian.mapper.CardRankTaskUserCompleteMapper;
import com.aiti.lulian.mapper.CardUserAuthenticationMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardUserRankTaskHistoryService;
import com.aiti.lulian.strategy.task.RankIncreaseTaskStrategy;
import com.aiti.lulian.vo.CardRankTaskUserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class RealNameAuthStrategy implements RankIncreaseTaskStrategy {

    @Autowired
    private CardUserAuthenticationMapper cardUserAuthenticationMapper;
    @Autowired
    private CardRankTaskUserCompleteMapper cardRankTaskUserCompleteMapper;
    @Autowired
    private CardUserRankTaskHistoryService cardUserRankTaskHistoryService;
    @Autowired
    private CardPersonalBaseInfoMapper cardBaseInfoMapper;

    @Override
    public RankIncreaseTaskStrategyEnum getStrategy() {
        return RankIncreaseTaskStrategyEnum.REAL_NAME_AUTH;
    }

    @Override
    public void handleTask(CardRankTaskUserVo vo, CardUserIncreaseRankDto dto) {
        List<CardUserAuthentication> cardUserAuthentications = cardUserAuthenticationMapper.selectList(new LambdaQueryWrapper<CardUserAuthentication>().eq(CardUserAuthentication::getUserId, dto.getUserId()));
        if(!CollectionUtils.isEmpty(cardUserAuthentications)){
            vo.setDoneFlag(true);
        }else{
            vo.setDoneFlag(false);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message handle(CardUserIncreaseRankDto dto) {
        String userId = dto.getUserId();
        String cardId = dto.getCardId();
        List<CardUserAuthentication> cardUserAuthentications = cardUserAuthenticationMapper.selectList(new LambdaQueryWrapper<CardUserAuthentication>().eq(CardUserAuthentication::getUserId, userId));
        if(!CollectionUtils.isEmpty(cardUserAuthentications)){

            String type = getStrategy().getType();
            CardRankTaskUserVo vo = new CardRankTaskUserVo();
            vo.setUserId(userId);
            vo.setCardId(cardId);
            vo.setType(type);
            List<CardRankTaskUserVo> cardRankTaskUserVos =  getCardRankTaskUserCompleteMapper().currentUserTaskCompleteListByUser(vo);
            if(CollectionUtils.isEmpty(cardRankTaskUserVos)){
                return Message.fail("任务不存在");
            }
            LambdaQueryWrapper<CardUserRankTaskHistory> wrapper = new LambdaQueryWrapper<CardUserRankTaskHistory>().eq(CardUserRankTaskHistory::getCardId, cardId)
                    .eq(CardUserRankTaskHistory::getType, RankIncreaseTaskStrategyEnum.REAL_NAME_AUTH.getType());
            List<CardUserRankTaskHistory> list = cardUserRankTaskHistoryService.list(wrapper);
            if(!CollectionUtils.isEmpty(list)){
                return Message.fail("该用户已领取过奖励，请勿重复领取");
            }

            CardRankTaskUserComplete cardRankTaskUserComplete = new CardRankTaskUserComplete();
            cardRankTaskUserComplete.setCardId(cardId);
            cardRankTaskUserComplete.setUserId(userId);
            cardRankTaskUserComplete.setTaskId(cardRankTaskUserVos.get(0).getBaseId());
            cardRankTaskUserCompleteMapper.insert(cardRankTaskUserComplete);

            Long score = cardRankTaskUserVos.get(0).getScore();
            dto.setScore(score);
            increseScore(dto);
            recordHistory(dto);


            return Message.success();


        }else{
            return Message.fail("该用户未实名认证，请先实名认证");
        }


    }

    @Override
    public CardRankTaskUserCompleteMapper getCardRankTaskUserCompleteMapper() {
        return cardRankTaskUserCompleteMapper;
    }

    @Override
    public CardPersonalBaseInfoMapper getBaseInfoMapper() {
        return cardBaseInfoMapper;
    }

    @Override
    public CardUserRankTaskHistoryService getHisotryService() {
        return cardUserRankTaskHistoryService;
    }


}